from django.shortcuts import render
import json
from django.http import HttpResponse, JsonResponse
from leave.models import LeaveInf
import datetime as dt


# 新请假
def create_leave(request):
    try:
        if request.method == 'POST':
            # uid = request.session['id']
            uid = 2022007185
            date = request.POST.get('date')
            time = request.POST.get('time')
            reason = request.POST.get('reason')
            ini_date = request.POST.get('ini_date')
            ini = dt.datetime.strptime(ini_date, '%Y-%m-%d').date()
            if LeaveInf.objects.filter(user_id=uid, ini_day=ini).first() is None:
                result = HttpResponse(json.dumps({'result': '1'}))
                LeaveInf.objects.create(user_id=uid, day=date, time=time, reason=reason, ini_day=ini_date)
            else:
                result = HttpResponse(json.dumps({'result': '0'}))
            result["Access-Control-Allow-Origin"] = "*"
            result["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
            result["Access-Control-Max-Age"] = "1000"
            return result
    except:
        result = HttpResponse(json.dumps({'result': '100'}))
        result["Access-Control-Allow-Origin"] = "*"
        result["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
        result["Access-Control-Max-Age"] = "1000"

        return result


def user_exit(request):  # 未使用

    return HttpResponse('退出成功')


# 请假信息显示
def leave_show(request):
    try:
        if request.method == 'GET':
            s_date = request.GET.get('s_date')
            e_date = request.GET.get('e_date')
            # uuid = request.session['id']
            uuid =2022007185
            date1 = dt.datetime.strptime(s_date, '%Y-%m-%d').date()
            date2 = dt.datetime.strptime(e_date, '%Y-%m-%d').date()
            re_inf = {}
            users = LeaveInf.objects.filter(user_id=uuid, ini_day__gte=date1, ini_day__lte=date2).order_by(
                '-ini_day').values('ini_day', 'day', 'time', 'reason')
            re_inf["data"] = list(users)
            re_inf['counts'] = len(re_inf['data'])
            result = JsonResponse(re_inf)
            result["Access-Control-Allow-Origin"] = "*"
            result["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
            result["Access-Control-Max-Age"] = "1000"
            return result
    except:
        re_inf['data'] = "还没请过假"
        re_inf['counts'] = 0
        result = JsonResponse("加载失败")
        result["Access-Control-Allow-Origin"] = "*"
        result["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
        result["Access-Control-Max-Age"] = "1000"
        return result


def leave_delete(request):
    try:
        uuid = 2022007186
        if request.method == 'POST':
            d_obj = LeaveInf.objects.filter(user_id=uuid).order_by('-ini_day')
            if d_obj:
                print(d_obj[0])
            else:
                print("123")
            d_obj[0].delete()
            result = HttpResponse(json.dumps({'result': '1'}))
            return result
    except:
        return HttpResponse(json.dumps({'result': '0'}))
